1675G - Sorting Pancakes - CodeForces Solution


dp *2300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std ;
//#define int long long 
#define endl '\n' 
const int inf = 1e9 ;
const int maxn = 253 ;
int dp[maxn][maxn][maxn] ;
int mi[maxn][maxn][maxn] ;
void solve() { 
    int n , m ; cin >> n >> m ;
    vector<int> a(n + 1) , s(n + 1);
    for(int i = 1;i<=n;i++) cin >> a[i] , s[i] = s[i - 1] + a[i] ;
    
    memset(dp , 63 , sizeof dp ) ;
    memset(mi , 63 , sizeof mi ) ;
    dp[0][0][m] = 0 ;
    for(int i = 1;i<=n;i++) { 
        for(int j = 0 ;j<=m;j++) { 
            mi[i - 1][j][m] = min(mi[i - 1][j][m] , dp[i - 1][j][m]) ;
            for(int k = m - 1;k >= 0 ;k--) { 
                mi[i - 1][j][k] = min(dp[i - 1][j][k] , mi[i - 1][j][k + 1]) ;
            }
        }
        for(int j = 0 ;j<=m;j++) { 
            for(int k = 0;k<=j;k++) { 
                dp[i][j][k] = min(dp[i][j][k] , mi[i - 1][j - k][k] + abs(s[i] - j))  ;
            }
        }
    }
    int ans = 1e9 ;
    for(int i = 0;i<=m;i++) {
        ans = min(ans , dp[n][m][i]) ;
    }
    cout<<ans<<endl;
}

signed main()  {
     int t = 1 ;
     //cin >> t ;
     while(t--)  {
        solve() ;
     }
}


Comments

Submit
0 Comments
More Questions

1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map
630I - Parking Lot
160B - Unlucky Ticket
371B - Fox Dividing Cheese
584B - Kolya and Tanya
137B - Permutation
550C - Divisibility by Eight
5A - Chat Servers Outgoing Traffic
615A - Bulbs
5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading
141B - Hopscotch
47B - Coins
1466C - Canine poetry
74A - Room Leader
1333D - Challenges in school №41
1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)